﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.CommandLine;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ls.Utils
{
    public static class ConfigHelper
    { 
        static ConfigHelper()
        {
            Init(new string[0]);
        }
        private static CommandLineConfigurationProvider cmdProvider = null; //= new CommandLineConfigurationProvider();
        private static IConfigurationRoot root = null;
        public static void Init(string[] args, string config = "appsettings.json")
        {
            if (args != null)
            {
                cmdProvider = new CommandLineConfigurationProvider(args);
            }

            if (string.IsNullOrEmpty(config))
            {
                config = "appsettings.json";
            }
            string path = Path.Combine(Directory.GetCurrentDirectory(), config);

            var builder = new ConfigurationBuilder();
            builder.AddJsonFile(path, true, true);
            root = builder.Build();
        }

        public static string GetString(string key)
        {
            return root[key];
        }
        public static int GetInt(string key)
        {
            return int.Parse(root[key]);
        }
             
        /// <summary>
        /// 获取Web.config或App.config的值（允许值不存在或为空时输出默认值）。
        /// </summary>
        public static string GetApp(string key)
        {
            return GetApp(key, "");
        }

        public static string GetApp(string key ,string defaultValue) {
            string value = root[key];
            return value.IsNullOrEmpty() ? defaultValue : value; 
        }

        /// <summary>
        /// 获取ConnectionStrings节点的值。
        /// </summary>
        public static string GetConn(string name)
        { 
            if (string.IsNullOrEmpty(name))
            {
                name = "Default";
            }
            if (name.Trim().Contains(" "))
            {
                return name;
            } 
            return root.GetConnectionString(name); 
        }

        public static bool GetAppBool(string key, bool defaultValue)
        {
            var value = GetApp(key);
            if (value.IsNullOrEmpty())
            {
                return defaultValue;
            }
            else
            {
                return CommonHelper.ToBool(value, defaultValue);
            }
        }
        public static int GetAppInt(string key, int defaultValue = 0)
        {
            var value = GetApp(key);
            if (value.IsNullOrEmpty())
            {
                return defaultValue;
            }
            else
            {
                return CommonHelper.ToInt(value, defaultValue);
            }
        }
         
        /// <summary>
        /// 启动是否清除插件目录
        /// </summary>
        public static bool ClearModuleShadowDirectoryOnStartup
        {
            get
            {
                return GetAppBool("ClearModuleShadowDirectoryOnStartup", true);
            }
        }

        public static T GetFromJson<T>(string config)
        {
             
            string path = Path.Combine(Directory.GetCurrentDirectory(), config);
            if (!File.Exists(path))
            {
                return default(T);
            }
            string data = File.ReadAllText(path, Encoding.UTF8);
            return JsonHelper.ToObject<T>(data);
        }
    }
}
